package com.example.administrator.base.activity;

import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.EditText;


import com.example.administrator.base.R;
import com.example.administrator.base.application.App;
import com.example.administrator.base.baseActivity.BaseActivity;
import com.example.administrator.base.bean.ReLogout;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.InputMethodUtil;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class NewPassword extends BaseActivity {

    @InjectView(R.id.Toolbar)
    android.support.v7.widget.Toolbar Toolbar;
    @InjectView(R.id.et_phone)
    EditText etPhone;
    @InjectView(R.id.et_code)
    EditText etCode;
    SweetAlertDialog sweetAlertDialog;
    String phone = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_password);
        ButterKnife.inject(this);
        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");
        sweetAlertDialog = new SweetAlertDialog(this, 5);
        sweetAlertDialog.setCanceledOnTouchOutside(false);
        sweetAlertDialog.setTitleText("加载中");
        etPhone.setFocusableInTouchMode(true);
        etPhone.requestFocus();
        InputMethodUtil.showInputMethod(etPhone, 998);
        Toolbar.setTitle("赳猎人");
        Toolbar.setSubtitle("重置密码");
        setSupportActionBar(Toolbar);
        Toolbar.setNavigationIcon(R.drawable.backbar);
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onResponseSuccess(Response response, String action) {
        ResponseBody body = response.body();
        if (body != null) {
            try {
                String string = body.string();
                final ReLogout mse = GsonTools.changeGsonToBean(string, ReLogout.class);
                if (mse != null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showToast("" + mse.getMessage());
                            sweetAlertDialog.dismiss();
                            finish();
                        }
                    });
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showToast("数据异常请重试");
                            sweetAlertDialog.dismiss();
                        }
                    });
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    sweetAlertDialog.dismissWithAnimation();
                    showToast("数据不完整，请稍后再试");
                }
            });
        }

    }

    @Override
    protected void onResponseFailure(Throwable e, String action) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast("网络异常请重试");
                sweetAlertDialog.dismiss();
            }
        });
    }

    @OnClick(R.id.btn_next)
    public void onViewClicked() {
        String password = etPhone.getText().toString();
        String newpassword = etCode.getText().toString();
        if (!password.equals(newpassword)) {
            showToast("两次输入的密码不一样");
        } else {
            sweetAlertDialog.show();
            getpassword(password);
        }
    }

    /*找回密码*/
    public void getpassword(String password) {
        RequestBody formBody = new FormBody.Builder()
                .add("action", "FINDPSW")
                .add("phone", phone)
                .add("newPassword", password)
                .build();
        final Request request = new Request.Builder()
                .url(App.getUrl())
                .post(formBody)
                .build();
        postRequest(request, "FINDPSW");

    }


}
